// Copyright 2010 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package utils;

import com.google.api.adwords.lib.AdWordsService;
import com.google.api.adwords.lib.AdWordsServiceLogger;
import com.google.api.adwords.lib.AdWordsUser;
import com.google.api.adwords.lib.ServiceAccountantManager;
import com.google.api.adwords.v13.AccountInfo;
import com.google.api.adwords.v13.AccountInterface;

import org.apache.log4j.Level;

/**
 * This demo shows how to use the {@link AdWordsServiceLogger} class to do
 * runtime logging configuration.
 */
public class LoggingDemo {
  public static void main(String[] args) throws Exception {
    // Get AdWordsUser from "~/adwords.properties".
    AdWordsUser user = new AdWordsUser();

    // Get AccountService.
    AccountInterface accountService =
        user.getService(AdWordsService.V13.ACCOUNT_SERVICE);

    // Initialize the root logger so that all messages do not filter up.
    AdWordsServiceLogger.initRootLogger();

    // Log the incoming and outgoing XML to System.out;
    AdWordsServiceLogger.logToStream(AdWordsServiceLogger.SOAP_XML_LOG, System.out);

    // Fake an exception to test logger which will not work yet.
    AdWordsServiceLogger.AW_API_LIB_LOG.error("Error in the library.",
        new NullPointerException());

    // Log errors of the AdWords library to System.err.
    AdWordsServiceLogger.logToStream(AdWordsServiceLogger.AW_API_LIB_LOG, System.err, Level.ERROR);

    // Fake an exception to test logger which will work now.
    AdWordsServiceLogger.AW_API_LIB_LOG.error("Error in the library.", new NullPointerException());

    // Displays the account's name, timezone, language, and currency.
    AccountInfo info = accountService.getAccountInfo();

    System.out.println("Account name is \"" + info.getDescriptiveName()
        + "\", id is \"" + info.getCustomerId() + "\", timezone is \""
        + info.getTimeZoneId() + "\", language is \""
        + info.getLanguagePreference() + "\", and currency is \""
        + info.getCurrencyCode() + "\".");

    // Determining how much quota all these operations have consumed.
    System.out.println("Total Quota unit cost for this run: "
        + ServiceAccountantManager.getInstance().getTotalUnitCount());
  }
}
